package com.bing.jh.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.jh.entity.TTfrDetailDay;
import com.bing.jh.entity.dto.TTfrDetailDayDTO;
import com.bing.jh.entity.vo.TTfrDetailDayVO;
import com.bing.jh.entity.vo.TTfrDetailReasonVO;
import com.bing.jh.enums.TTfrDetailDayEnums;
import com.bing.jh.mapper.TTfrDetailDayMapper;
import com.bing.jh.service.TTfrDetailDayService;
import org.springframework.stereotype.Service;

import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 29821
 * @description 针对表【t_tfr_detail_day(终端故障率日粒度报表)】的数据库操作Service实现
 * @createDate 2024-08-09 11:42:25
 */
@Service
public class TTfrDetailDayServiceImpl extends ServiceImpl<TTfrDetailDayMapper, TTfrDetailDay>
        implements TTfrDetailDayService {

    @Override
    public List<TTfrDetailDayVO> list(TTfrDetailDayDTO tTfrDetailDayDTO) {
        String flag = Objects.requireNonNull(TTfrDetailDayEnums.fromCode(tTfrDetailDayDTO.getFlag())).getType();
        tTfrDetailDayDTO.setType(flag);
        return baseMapper.queryYearList(tTfrDetailDayDTO);
    }

    @Override
    public List<String> dateList() {
        int currentYear = Year.now().getValue();
        return baseMapper.dateList().stream()
                .filter(Objects::nonNull)
                .filter(date -> date.matches("2\\d{3}"))
                .filter(date -> {
                    try {
                        return Integer.parseInt(date) <= currentYear;
                    } catch (NumberFormatException e) {
                        return false;
                    }
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<TTfrDetailReasonVO> queryReasonDetail(TTfrDetailDayDTO tTfrDetailDayDTO) {
        String flag = Objects.requireNonNull(TTfrDetailDayEnums.fromCode(tTfrDetailDayDTO.getFlag())).getType();
        tTfrDetailDayDTO.setType(flag);
        String startTime = tTfrDetailDayDTO.getStartTime();
        String endTime = tTfrDetailDayDTO.getEndTime();
        List<String> monthRange = getMonthRange(startTime, endTime);
        tTfrDetailDayDTO.setTime(monthRange);
        return baseMapper.queryReasonDetail(tTfrDetailDayDTO);
    }

    @Override
    public List<String> detailDate() {
        YearMonth currentYearMonth = YearMonth.now();

        return baseMapper.detailDate().stream()
                .filter(Objects::nonNull)
                .filter(date -> date.matches("2\\d{5}"))
                .filter(date -> {
                    try {
                        YearMonth ym = YearMonth.parse(date, DateTimeFormatter.ofPattern("yyyyMM"));
                        return ym.compareTo(currentYearMonth) <= 0;
                    } catch (DateTimeParseException e) {
                        return false;
                    }
                })
                .map(date -> YearMonth.parse(date, DateTimeFormatter.ofPattern("yyyyMM"))
                        .format(DateTimeFormatter.ofPattern("yyyy-MM")))
                .collect(Collectors.toList());
    }

    public static List<String> getMonthRange(String startTime, String endTime) {
        List<String> months = new ArrayList<>();
        YearMonth start = YearMonth.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM"));
        YearMonth end = YearMonth.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM"));

        while (!start.isAfter(end)) {
            months.add(start.format(DateTimeFormatter.ofPattern("yyyy-MM")));
            start = start.plusMonths(1);
        }

        return months.stream()
                .map(date -> date.replace("-", ""))
                .collect(Collectors.toList());
    }

}




